package org.jeecg.modules.demo.hydrology.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.demo.hydrology.dto.JslcddyDto;
import org.jeecg.modules.demo.hydrology.dto.JslcdssDto;
import org.jeecg.modules.demo.hydrology.entity.Jslcddy;
import org.jeecg.modules.demo.hydrology.entity.Jslcdss;
import org.jeecg.modules.demo.hydrology.mapper.JslcddyMapper;
import org.jeecg.modules.demo.hydrology.mapper.JslcdssMapper;
import org.jeecg.modules.demo.hydrology.service.IJslcdssService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysDict;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.mapper.SysDepartMapper;
import org.jeecg.modules.system.mapper.SysDictItemMapper;
import org.jeecg.modules.system.mapper.SysDictMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 降水量实时
 * @Author: jeecg-boot
 * @Date:   2024-04-17
 * @Version: V1.0
 */
@Service
public class JslcdssServiceImpl extends ServiceImpl<JslcdssMapper, Jslcdss> implements IJslcdssService {
    @Autowired
    private JslcdssMapper jslcdssMapper;
    @Autowired
    private SysDepartMapper sysDepartMapper;
    @Autowired
    private SysDictMapper sysDictMapper;
    @Autowired
    private SysDictItemMapper sysDictItemMapper;
    /**
     * 降水量实时-分页列表查询
     * @param page
     * @param queryWrapper
     * @return
     */
    @Override
    public IPage<JslcdssDto> getList(Page<Jslcdss> page, QueryWrapper<Jslcdss> queryWrapper) {
        Page<Jslcdss> jslcdssPage = jslcdssMapper.selectPage(page, queryWrapper);
        List<JslcdssDto> collect = jslcdssPage.getRecords().stream().map(jslcdss -> {
            JslcdssDto jslcdssDto = new JslcdssDto();
            BeanUtils.copyProperties(jslcdss, jslcdssDto);
            QueryWrapper<SysDepart> sysDepartQueryWrapper = new QueryWrapper<>();
            sysDepartQueryWrapper.eq("org_code", jslcdss.getStationMine());
            SysDepart sysDepart = sysDepartMapper.selectOne(sysDepartQueryWrapper);
            if (sysDepart != null){
                jslcdssDto.setStationName(sysDepart.getDepartName());
            }
            QueryWrapper<SysDict> sysDictQueryWrapper = new QueryWrapper<>();
            sysDictQueryWrapper.eq("dict_code","point_status");
            SysDict sysDict = sysDictMapper.selectOne(sysDictQueryWrapper);
            if (sysDict != null){
                QueryWrapper<SysDictItem> sysDictItemQueryWrapper = new QueryWrapper<>();
                sysDictItemQueryWrapper.eq("dict_id",sysDict.getId()).eq("item_value",jslcdss.getStationStatus());
                SysDictItem sysDictItem = sysDictItemMapper.selectOne(sysDictItemQueryWrapper);
                if (sysDictItem != null) {
                    jslcdssDto.setPointStatusItemText(sysDictItem.getItemText());
                }
            }
            return jslcdssDto;
        }).collect(Collectors.toList());
        IPage<JslcdssDto> jslcdssDtoIPage = new Page<>();
        jslcdssDtoIPage.setRecords(collect);
        jslcdssDtoIPage.setTotal(jslcdssPage.getTotal());
        jslcdssDtoIPage.setPages(page.getPages());
        jslcdssDtoIPage.setSize(page.getSize());
        return jslcdssDtoIPage;
    }
}
